package com.isunland.managesystem.receiver;

import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import cn.jpush.android.api.JPushInterface;
import com.isunland.managesystem.R;
import com.isunland.managesystem.entity.BookOrderInfo;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.ui.AgriculturalForumMyReplyListActivity;
import com.isunland.managesystem.ui.AgriculturalForumMyReplyListFragment;
import com.isunland.managesystem.ui.AgricultureServiceActivity;
import com.isunland.managesystem.ui.AgricultureServiceFragment;
import com.isunland.managesystem.ui.AppointmentOrderPagerActivity;
import com.isunland.managesystem.ui.CommonMessageListActivity;
import com.isunland.managesystem.ui.LocaleWorkQueryCountListActivity;
import com.isunland.managesystem.ui.OnlineReceiveOrderListActivity;
import com.isunland.managesystem.ui.SplashActivity;
import com.isunland.managesystem.utils.LogUtil;
import com.isunland.managesystem.utils.MyStringUtil;
import com.isunland.managesystem.utils.SharedPreferencesUtil;
import org.json.JSONObject;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: wangyanan
 * 创建时间: 2017/3/28 14:38
 */

public class JPushReceiver extends BroadcastReceiver {
  private static final String TAG = JPushReceiver.class.getSimpleName() + "JPushReceiver";
  /**
   * 公共消息
   */
  private static final String CODE_COMMON_MESSAGE = "100";
  /**
   * 预约提交
   */
  private static final String CODE_ORDER_SUBMIT = "110";
  /**
   * 农机组织接单
   */
  private static final String CODE_ORDER_RECEIVE = "111";
  /**
   * 农户确认订单
   */
  private static final String CODE_ORDER_RECEIVE_SUCCESS = "112";
  /**
   * 农技服务
   */
  private static final String CODE_AGRICULTURAL_TECHNOLOGY = "119";
  /**
   * 农机服务
   */
  private static final String CODE_AGRICULTURAL_MACHINERY = "120";
  /**
   * 农技服务 我的回复
   */
  private static final String CODE_AGRICULTURAL_TECHNOLOGY_REPLY = "121";
  /**
   * 农机服务 我的回复
   */
  private static final String CODE_AGRICULTURAL_MACHINERY_REPLY = "122";
  private static final String CODE_LOCALE_WORK = "130";
  private static final String CODE_URL = "140";

  private NotificationManager nm;

  @Override public void onReceive(Context context, Intent intent) {
    if (null == nm) {
      nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    }

    Bundle bundle = intent.getExtras();
    LogUtil.d(TAG, "onReceive - " + intent.getAction() + ", extras: " + bundle.toString());

    if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
      LogUtil.d(TAG, "JPush用户注册成功");
    } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
      LogUtil.d(TAG, "接受到推送下来的自定义消息");
    } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
      LogUtil.d(TAG, "接受到推送下来的通知");

      receivingNotification(context, bundle);
    } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
      LogUtil.d(TAG, "用户点击打开了通知");

      openNotification(context, bundle);
    } else {
      LogUtil.d(TAG, "Unhandled intent - " + intent.getAction());
    }
  }

  @SuppressWarnings("all") private void receivingNotification(Context context, Bundle bundle) {
    String title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
    LogUtil.d(TAG, " title : " + title);
    String message = bundle.getString(JPushInterface.EXTRA_ALERT);
    LogUtil.d(TAG, "message : " + message);
    String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
    LogUtil.d(TAG, "extras : " + extras);
  }

  /**
   * 点击通知回调的方法,
   */
  private void openNotification(Context context, Bundle bundle) {
    /** 获取的额外信息*/
    String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
    /** 取出code进行判断*/
    String code = null;
    String id = null;
    String url = null;
    try {
      JSONObject extrasJson = new JSONObject(extras);
      code = extrasJson.optString("code");
      id = extrasJson.optString("id");
      url = extrasJson.optString("url");
    } catch (Exception e) {
      LogUtil.e(TAG, "Unexpected: extras is not a valid json", e);
    }
    if (code == null) code = "";
    if (id == null) id = "";
    if (url == null) url = "";

    //if (BuildConfig.DEBUG){
    //    ToastUtil.showLong("code="+code+",\n id="+id+"\n ----其他---- \n bundle="+bundle);
    //}
    Intent mIntent;
    switch (code) {
      case CODE_COMMON_MESSAGE:
        mIntent = new Intent(context, CommonMessageListActivity.class);
        mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(mIntent);
        break;

      case CODE_ORDER_SUBMIT:
        mIntent = new Intent(context, OnlineReceiveOrderListActivity.class);
        mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(mIntent);
        break;
      case CODE_LOCALE_WORK:
        mIntent = new Intent(context, LocaleWorkQueryCountListActivity.class);
        mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(mIntent);
        break;
      case CODE_ORDER_RECEIVE:

        BookOrderInfo item = new BookOrderInfo();
        item.setId(id);
        mIntent = new Intent(context, AppointmentOrderPagerActivity.class);
        mIntent.putExtra(AppointmentOrderPagerActivity.EXTRA_TASK, item);
        mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(mIntent);
        break;

      case CODE_ORDER_RECEIVE_SUCCESS:
        mIntent = new Intent(context, OnlineReceiveOrderListActivity.class);
        mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(mIntent);
        break;
      case CODE_AGRICULTURAL_MACHINERY:
        //农机
        SharedPreferencesUtil.setString(context, SharedPreferencesUtil.KEY_FORUM_SUBCODE, "02");
        mIntent = new Intent(context, AgricultureServiceActivity.class);
        //mIntent.putExtra(AgricultureServiceFragment.EXTRA_TYPE, "02");
        mIntent.putExtra(AgricultureServiceFragment.EXTRA_TITLE,
            context.getString(R.string.trechForum));
        mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(mIntent);
        break;
      case CODE_AGRICULTURAL_TECHNOLOGY:
        //农技
        SharedPreferencesUtil.setString(context, SharedPreferencesUtil.KEY_FORUM_SUBCODE, "01");
        mIntent = new Intent(context, AgricultureServiceActivity.class);
        //mIntent.putExtra(AgricultureServiceFragment.EXTRA_TYPE, "01");
        mIntent.putExtra(AgricultureServiceFragment.EXTRA_TITLE,
            context.getString(R.string.AgricultureService));
        mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(mIntent);
        break;
      case CODE_AGRICULTURAL_TECHNOLOGY_REPLY:
        //农技: 我的回复
        SharedPreferencesUtil.setString(context, SharedPreferencesUtil.KEY_FORUM_SUBCODE, "01");
        mIntent = new Intent(context, AgriculturalForumMyReplyListActivity.class);
        mIntent.putExtra(AgriculturalForumMyReplyListFragment.EXTRA_JOBNO,
            CurrentUser.newInstance(context).getJobNumber());
        mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(mIntent);
        break;
      case CODE_AGRICULTURAL_MACHINERY_REPLY:
        //农机: 我的回复
        SharedPreferencesUtil.setString(context, SharedPreferencesUtil.KEY_FORUM_SUBCODE, "02");
        mIntent = new Intent(context, AgriculturalForumMyReplyListActivity.class);
        mIntent.putExtra(AgriculturalForumMyReplyListFragment.EXTRA_JOBNO,
            CurrentUser.newInstance(context).getJobNumber());
        mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(mIntent);
        break;
      case CODE_URL:
        //url打开浏览器.
        //从浏览器打开
        if (MyStringUtil.isEmpty(url)) return;
        try {
          mIntent = new Intent();
          mIntent.setAction(Intent.ACTION_VIEW);
          mIntent.setData(Uri.parse(url));
          mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          context.startActivity(mIntent);
        } catch (Exception e) {
          e.printStackTrace();
        }
        break;
      default:
        mIntent = new Intent(context, SplashActivity.class);
        mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(mIntent);
        break;
    }
  }
}
